/*
    3. Write a C program to sort a list of elements
     using the bubble sort algorithm.
    Bubble Sort works by repeatedly swapping 
    the adjacent elements if they are in wrong order.
*/

#include <stdio.h>
#include <stdlib.h>

void swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

void bubblesort(int a[], int len)
{
    int i, j;
    for(j = 0; j < len - 1; j++){
        for(i = 0; i < len - 1 - j; i++){
            if(a[i] > a[i + 1]){
                swap(&a[i], &a[i + 1]);
            }
        }
    }    
}

int main(void)
{
    int alen;
    int i;
    printf("Input the number of array element : ");
    scanf("%d", &alen);
    int *a = (int *)malloc(alen * sizeof(int));
    for(i = 0; i < alen; i++){
        scanf("%d", &a[i]);
    }
    bubblesort(a, alen);
    for(i = 0; i < alen; i++){
        printf("%d, ", a[i]);
    }

    return 0;
}